package com.okay.popwindow;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import com.okay.testclass.R;
import java.util.ArrayList;
import java.util.List;

/**
 * 一个popwindow的demo
 */
public class PopActivity extends FragmentActivity {
    PopWindowView popWindowView;
    List<ClassRelation> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop);
        Button button = findViewById(R.id.button);
        Button button1 = findViewById(R.id.button1);
        RelativeLayout relative = findViewById(R.id.relative);
        list = new ArrayList<>();
//
//        data class ClassRelation(
//                val class_id: Long,
//                val class_type: Int,
//                val class_name: String,
//                val sn: String,
//                var userId: String?,
//                var isJoinRoom: Boolean = false,
//                var onLine: Boolean = false
//        )


        for(int i=0;i<10;i++){
            ClassRelation poData;
            poData = new ClassRelation(2,1,"初中1325机构学校一二"+i,"1111"+i,"@12234",false,true);
            list.add(poData);
        }

        button.setOnClickListener(view -> {
            popWindowView  =new PopWindowView();
            popWindowView.initPopWindow(this, relative, button, list, new PopWindowView.IMsgPopClick() {
               @Override
               public void onMsgPopClick(Object data) {
                   Toast.makeText(PopActivity.this,"选中"+((ClassRelation)data).getClass_name(),Toast.LENGTH_SHORT).show();
               }
           });
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        Thread.sleep(2000);
//                        Message message  = new Message();
//                        message.what =1;
//                        handler.sendMessage(message);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }).start();
        });


//        button1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                List<SnListData> list = new ArrayList<>();
//                for(int i=0;i<7;i++){
//                    SnListData poData;
//                    poData = new SnListData(2,"李四"+i,"222"+i);
//                    list.add(poData);
//                }
//                if(popWindowView!=null){
//                    popWindowView.freshData(list);
//                }
//            }
//        });
    }
    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            ClassRelation poData =null;
            for (ClassRelation ss:list) {
               if(ss.getClass_name().equals("张三2")){
                   poData = ss;
               }
            }
            if(poData!=null){
                list.remove(poData);
            }
                if(popWindowView!=null){
                    popWindowView.freshData(list);
                }
        }
    };
}
